package Demo2_18;

import java.util.ArrayDeque;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-02-18
 * Time:19:06
 */
public class BTree {
    static class TreeNode {
        public char val;
        //存储左孩子节点的引用
        public TreeNode left;
        //存储右孩子节点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void preOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        ArrayDeque<Integer> s = new ArrayDeque<>();
        s.push(1);
        TreeNode cur = root;
        TreeNode top = null;

        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }
        System.out.println();
    }

}
